<?php

namespace App\Http\Controllers\Site;

use App\B2BProject\Site\HelperControllerBuildTrait;
use Auth;
use App\B2BProject\Area;
use App\B2BProject\Custom\Business\Goods;
use App\B2BProject\Custom\Business\GoodsCategory;
use App\B2BProject\Custom\Business\GoodsImage;
use App\B2BProject\Custom\Business\Store;
use App\B2BProject\Custom\MemberInformation;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\B2BProject\Exception\UserTypeException;

class FreeController extends Controller
{

    use HelperControllerBuildTrait;

    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth', ['except' => ['getGoods', 'getChoose', 'getList']]);
    }

    /**
     * 显示列表
     * @return \Illuminate\View\View
     */
    public function getList(Request $request)
    {
        $cid = '';
        $pid = 0;
        return view('host/free')->with('cid', $cid)->with('pid', $pid);
    }

    /**
     * 首页跳转
     * @param $cid
     * @param $pid
     * @return $this
     */
    public function getChoose($cid, $pid)
    {
        return view('host/free')->with('pid', $pid)->with('cid', $cid);
    }

    /**
     * 读取商品的数据 包括图片的数据 商家的信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGoods(Request $request)
    {
        $request->query->set('page', $request->query('page', $request->page));
        $request->query->set('pageSize', $request->query('pageSize', $request->pageSize));

        $query = Goods::display()->hasCover()->where('grant_status', false)->where('strict_lock',false);

        if ($goodType = $request->query('goodType')) {
            $category = GoodsCategory::with('children')->whereId($goodType)->firstOrFail();
            if ($category->parent_id == 0) {
                $in = [];
                foreach ($category->children as $child) {
                    $in[] = $child->id;
                }

                $query = $query->whereIn('class_id', $in);
            } else {
                $query = $query->where('class_id', $goodType);
            }
        }
        if ($request->query('time')) {
            $query = $query->orderBy('created_at', 'asc');
        } else {
            $query = $query->orderBy('created_at', 'desc');
        }

        if ($addr = $request->query('addr')) {
            $query = $query->where('province_id', $addr);
        }

        if ($name = $request->query('name')) {
            $query = $query->where('name', 'like', "%$name%");
        }

        $goodsInfo = $query->paginate($request->query('pageSize', 7));

        $data = [];
        foreach ($goodsInfo as $info) {

            $item['goods_id'] = $info->id;
            $item['img']      = $info->cover();
            $item['name']     = $info->name;
            $item['href']     = route('goods.detail', ['id' => $info->id, 'dis_id' => 1]);
            $item['warrant']  = $info->isGranted();
            $item['price']    = $info->original_price;
            $item['minimum']  = $info->minimum_order;
            $item['supplier'] = $info->store->member->information->company;
            $item['store']    = $info->store->first()->id;
            $item['addr']     = Area::where('id', $info->province_id)->first()->name . Area::where('id',
                    $info->city_id)->first()->name;

            $data[] = $item;
        }
        return response()->json([
            'data'           => $data,
            'pagingConfData' => [
                'page'     => $goodsInfo->currentPage(),
                'pageSize' => $request->query('pageSize'),
                'total'    => $goodsInfo->total(),
            ],
        ]);
    }

}
